<?php
/*
 * 处理回复信息的类，可以由Message创建
 */
abstract class ReplyMessage {
	/*
	 * 创建一个ReplyMessage实例
	 */
	public static function getInstance($type) {	
		switch ($type) {
			case message_type_text:
				$msg = new TextReplyMessage();
				break;
			case message_type_music:
				$msg = new MusicReplyMessage();
				break;
			case message_type_news:
				$msg = new NewsReplyMessage();
				break;
			case message_type_service:
				$msg = new transferCustomerServiceReplyMessage();
				break;
			case message_type_image:
				$msg = new ImageReplyMessage();
				break;
			default:
				throw_exception('不存在的ReplyMessage类型');
		}
		$msg->createTime = date('Y-m-d H:i:s');
		$msg->msgType = $type;
		return $msg;
	}
	
	/*
	 * 
	 */
	public function to_xml_string() {
		$xml = array();
		
		$this->xml_append($xml, '<xml>');
		$this->xml_append($xml, '<ToUserName><![CDATA[' . $this->toUserName . ']]></ToUserName>');
		$this->xml_append($xml, '<FromUserName><![CDATA[' . $this->fromUserName . ']]></FromUserName>');
		$this->xml_append($xml, '<CreateTime>' . $this->createTime . '</CreateTime>');
		$this->xml_append($xml, '<MsgType><![CDATA[' . $this->msgType . ']]></MsgType>');
		
		$this->xml_content($xml);
		
		$this->xml_append($xml, '<FuncFlag>' . $this->funcFlag . '</FuncFlag>');
		$this->xml_append($xml, '</xml>');
		return implode("",$xml);
	}
	 
	protected function xml_content(&$xml) {
		throw_exception('ReplyMessage::xml_content必须被重载。');
	}
	
	protected function xml_append(&$xml, $str) {
		array_push($xml, $str);
	}
	/*
	 *
	*/
	public $fromUserName = '';
	
	/*
	 *
	*/
	public $toUserName = '';
	
	/*
	 *
	*/
	public $createTime = NULL;
	
	/*
	 *
	*/
	public $msgType = '';
	
	/*
	 *
	*/
	public $funcFlag = 0;
	
}

class TextReplyMessage extends ReplyMessage {
	public $content = '';
	
	public function xml_content(&$xml) {
		$this->xml_append($xml, '<Content><![CDATA[' . $this->content . ']]></Content>');
	}
}
class ImageReplyMessage extends ReplyMessage {
	public $content = '';

	public function xml_content(&$xml) {
		$this->xml_append($xml, '<Image>');
		$this->xml_append($xml, '<MediaId><![CDATA[' . $this->media_id . ']]></MediaId>');

		$this->xml_append($xml, '</Image>');
	}
}
class transferCustomerServiceReplyMessage extends ReplyMessage {
	protected function xml_content(&$xml) {
		//$this->xml_append($xml, '');
	}
}
class MusicReplyMessage extends ReplyMessage {
	public $title = '';
	public $description = '';
	public $musicUrl = '';
	public $hqMusicUrl = '';
	
	protected function xml_content(&$xml) {
		$this->xml_append($xml, '<Music>');
		$this->xml_append($xml, '<Title><![CDATA[' . $this->title . ']]></Title>');
		$this->xml_append($xml, '<Description><![CDATA[' . $this->description . ']]></Description>');
		$this->xml_append($xml, '<MusicUrl><![CDATA[' . $this->musicUrl . ']]></MusicUrl>');
		$this->xml_append($xml, '<HQMusicUrl><![CDATA[' . $this->hqMusicUrl . ']]></HQMusicUrl>');
		
		$this->xml_append($xml, '</Music>');
	}
}

class NewsReplyMessage extends ReplyMessage {
	public $articles = array(array('title' => '', 'description' => '', 'picUrl' => '', 'url' => ''));

	protected function xml_content(&$xml) {
		$this->xml_append($xml, '<ArticleCount>' . count($this->articles) . '</ArticleCount>');
		
		$this->xml_append($xml, '<Articles>');
		foreach ($this->articles as $item) {
			
			$this->xml_append($xml, '<item>');
			
			$this->xml_append($xml, '<Title><![CDATA[' . $item->title . ']]></Title>');
			$this->xml_append($xml, '<Description><![CDATA[' . $item->description . ']]></Description>');
			$this->xml_append($xml, '<PicUrl><![CDATA[' . $item->picUrl . ']]></PicUrl>');
			$this->xml_append($xml, '<Url><![CDATA[' . $item->url . ']]></Url>');
			
			$this->xml_append($xml, '</item>');
		}
		
		$this->xml_append($xml, '</Articles>');
	}
}
